/*
 * @Author: zhishiqu
 * @Description: 
 * @Date: 2023-05-11 17:18:41
 * @LastEditTime: 2023-05-12 11:57:11
 * @FilePath: \vue_cesium\src\views\webFlood\utils.js
 */
import {Simulation} from './simulation'

import {Visualisation} from './visualisation'

export function fileLoad(scenarioName) {
    var scenario = {
        "simulation": {
            "diff_alt": 0.035,
            "resolution": 512,
            "terrain2": "http://192.168.2.196:3001/WebFlood/scenarios/iowa/iowa_terrain.tif",
            "terrain": "http://192.168.2.196:3001/WebFlood/scenarios/iowa/ccc.tif",
            "worldSize": 8444,
            "heightScale": 10000,
            "dt": 0.25,
            "gravity": 9.81,
            "manningCoefficient": 0.57,
            "minFluxArea": 0.01,
            "frictionMap": "http://192.168.2.196:3001/WebFlood/scenarios/iowa/iowa_friction_better.tif",
            "sourceWaterHeight": 7.5,
            "sourceWaterVelocity": 0.5
        },
        "visualisation": {
            "satellite": "http://192.168.2.196:3001/WebFlood/scenarios/iowa/ccc.png",
            "displayHeightScale": 3,
            "maxDisplayWaterHeight": 5,  
            "flowStrength": 1000,
            "flowLength": 50
        }
    };

    var nMapsToLoad = 0;

    ["terrain", "water", "frictionMap"].forEach(function (map) {
        var mapUrl = scenario.simulation[map];
        if (mapUrl) {
            nMapsToLoad++;
            var xhr = new XMLHttpRequest();
            xhr.open('GET', mapUrl, true);
            xhr.responseType = "arraybuffer";
            xhr.onload = function () {
                mapLoaded(map, "simulation", tiffData(this.response));
            };
            xhr.send();
        }
    });

    var satUrl = scenario.visualisation.satellite;
    if (satUrl) {
        nMapsToLoad++;
        var xhr = new XMLHttpRequest();
        xhr.open('GET', satUrl, true);
        xhr.responseType = "blob";
        xhr.onload = function () {
            var satImage = new Image();
            satImage.src = URL.createObjectURL(this.response);
            satImage.onload = function () { mapLoaded("satellite", "visualisation", satImage); }
        };
        xhr.send();
    }

    var mapLoaded = function (name, target, data) {
        scenario[target][name] = data;  // 把scenarios对象里面的tiff文件以数据格式保存在该对象里面
        console.log(name, "loaded");
        if (--nMapsToLoad === 0) {
            console.log(scenario);
            Simulation.init(scenario.simulation);
            window.Simulation = Simulation
            Visualisation.init(Simulation, scenario.visualisation);
            window.Visualisation = Visualisation
        }
    }
}